package com.wecon.dianping.controller;

import com.wecon.dianping.common.*;
import com.wecon.dianping.model.User;
import com.wecon.dianping.request.LoginReq;
import com.wecon.dianping.request.RegisterReq;
import com.wecon.dianping.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.UnsupportedEncodingException;
import java.security.NoSuchAlgorithmException;

/**
 * @author Weikang  Wang
 * @program: com.wecon.dianping.controller
 * @description:
 * @date 2020/3/10 16:56
 */
@RestController
public class UserController {

    public static final String CURRENT_USER_SESSION = "currentUserSession";

    @Autowired
    private HttpServletRequest httpServletRequest;

    @Autowired
    private UserService userService;

    /**
     * 获取用户信息
     *
     * @param id /
     * @return /
     * @throws BusinessException /
     */
    @RequestMapping(value = "/getUser")
    public CommonResult getUser(@RequestParam(name = "id") Integer id) throws BusinessException {
        User user = userService.getUser(id);

        if (null == user) {
            throw new BusinessException(EmBusinessError.NO_OBJECT_FOUND);
        }

        return CommonResult.create(user);
    }

    @RequestMapping(value = "/index")
    public ModelAndView index() {
        String userName = "Spade";
        ModelAndView modelAndView = new ModelAndView("/index.html");
        modelAndView.addObject("name", userName);

        return modelAndView;
    }

    /**
     * 用户注册
     *
     * @param registerReq /
     * @return /
     */
    @RequestMapping(value = "/register")
    public CommonResult register(@Valid @RequestBody RegisterReq registerReq, BindingResult bindingResult) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }

        User user = new User();
        user.setTelphone(registerReq.getTelphone());
        user.setPassword(registerReq.getPassword());
        user.setNickName(registerReq.getNickName());
        user.setGender(registerReq.getGender());

        User register = userService.register(user);

        return CommonResult.create(register);
    }

    /**
     * 用户登录
     *
     * @param loginReq      登录参数
     * @param bindingResult /
     * @return /
     * @throws BusinessException            /
     * @throws UnsupportedEncodingException /
     * @throws NoSuchAlgorithmException     /
     */
    @RequestMapping(value = "/login")
    public CommonResult login(@Valid @RequestBody LoginReq loginReq, BindingResult bindingResult) throws BusinessException, UnsupportedEncodingException, NoSuchAlgorithmException {
        if (bindingResult.hasErrors()) {
            throw new BusinessException(EmBusinessError.PARMETER_VALIDATION_ERROR, CommonUtil.processErrorString(bindingResult));
        }

        String telphone = loginReq.getTelphone();
        String password = loginReq.getPassword();
        User user = userService.login(telphone, password);
        // 放入session
        httpServletRequest.getSession().setAttribute(CURRENT_USER_SESSION, user);

        return CommonResult.create(user);
    }

    /**
     * 用户登出
     * @return /
     */
    @RequestMapping(value = "/logout")
    public CommonResult login() {
        // 注销
        httpServletRequest.getSession().invalidate();

        return CommonResult.create(null);
    }

    /**
     * 获取当前用户信息
     * @return /
     */
    @RequestMapping(value = "/getCurrentUser")
    public CommonResult getCurrentUser(){
        User user = (User) httpServletRequest.getSession().getAttribute(CURRENT_USER_SESSION);

        return CommonResult.create(user);
    }

}
